import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from "path"

export default ({ mode }) =>{
  const root = process.cwd();
  const env = loadEnv(mode, root);
  const { VITE_PORT, VITE_BASE_URL,  VITE_API_URL, VITE_DROP_CONSOLE } = env;

	return defineConfig({ 
    plugins: [vue()],
    base: VITE_BASE_URL,
    server: {
      port: parseInt(VITE_PORT),
      proxy: {
        '/api': {
          target: VITE_API_URL,
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    },
    build: {
      target: 'es2015',
      outDir: 'dist',
      terserOptions: {
        compress: {
          keep_infinity: true,
          drop_console: VITE_DROP_CONSOLE as unknown as boolean,
        },
      },
      brotliSize: false,
      chunkSizeWarningLimit: 2000,
    },
    // 路径别名
    resolve: {
      alias: {
        "@": resovePath("src"),
        "@views": resovePath("src/views"),
        "@comps": resovePath("src/components"),
        "@imgs": resovePath("src/assets/img"),
        "@icons": resovePath("src/assets/icons"),
        "@utils": resovePath("src/utils"),
        "@stores": resovePath("src/store"),
        "@plugins": resovePath("src/plugins"),
        "@styles": resovePath("src/assets/styles"),
      }
    },
    css: {
      preprocessorOptions: {
        // sass variable and mixin
        scss: {
          additionalData: `@import "@styles/variables.scss"; @import "@styles/mixin.scss";`
        }
      }
    },
  })
}

function resovePath(paths) {
  return path.resolve(__dirname, paths)
}
